package com.benyi.byiee.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.benyi.byiee.dao.mappers.CountryAreaMapper;
import com.benyi.byiee.model.db.CountryArea;
import com.benyi.util.Config;
import com.benyi.util.bean.ServiceException;

import jakarta.annotation.Resource;

@Service
public class CountryAreaService {
private static Logger log = LoggerFactory.getLogger(CountryAreaService.class);
	
	@Resource(name="config")
	private Config config; 
	
	@Autowired
	CountryAreaMapper areaMapper;
	
	public List<CountryArea> queryList(Integer continent) {
		int c = 0;
		if(continent != null) {
			c = continent;
		}
		List<CountryArea> list = areaMapper.queryList(c);
		return list;
	}
	
	public CountryArea save(CountryArea v) throws ServiceException {
		String code = v.getAcode();
		if(code==null || code.length()<1) {
			throw new ServiceException("null_code","Code can not be null");
		}
		try {
			CountryArea c = areaMapper.selectByPrimaryKey(code); 
			if(c==null) {
				areaMapper.insert(v);
			}else {
				areaMapper.updateByPrimaryKey(v);
			} 
			c = areaMapper.selectByPrimaryKey(code);
			return c;
		}catch(Exception e) {
			log.info("Exception", e);
			throw new ServiceException("save-exception",e.getMessage());
		} 
	}
	
	public int delete(CountryArea v) throws ServiceException { 
		try { 
			int r = areaMapper.deleteByPrimaryKey(v.getAcode());  
			return r; 
		}catch(Exception e) {
			log.info("Exception", e);
			throw new ServiceException("cannot_remove",e.getMessage());
		} 
	}
	
	public CountryArea selectByCode(String code) {  
		CountryArea c = areaMapper.selectByPrimaryKey(code);  
		return c; 
	}
	
}
